package org.sanmao.wyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.sanmao.wyj.controller.utils.Result;
import org.sanmao.wyj.dao.UserDao;
import org.sanmao.wyj.domain.Message;
import org.sanmao.wyj.domain.User;
import org.sanmao.wyj.exception.AccountIsExistException;
import org.sanmao.wyj.service.MessageService;
import org.sanmao.wyj.service.UserService;
import org.sanmao.wyj.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.nio.channels.AcceptPendingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements UserService {

    @Autowired
    UserDao userDao;
    @Autowired
    MessageService messageService;

    @Override
    public List<User> findAllUsers() {
        List<User> users = userDao.selectList(null);
        return users;
    }

    @Override
    public IPage<User> findPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        userDao.selectPage(page,null);
        return page;
    }


    @Override
    public User findUserById(Integer id) {
        User user = userDao.getById(id);
        return user;
    }

    @Override
    public Boolean saveUser(User user) {
        String username = user.getUsername();
        User existUser = userDao.findUserOnlyByUsername(username);
        try{
            if(existUser != null){
                throw new AccountIsExistException("账号已存在！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
        int i = userDao.insert(user);
        return i > 0;
    }

    @Override
    public Boolean deleteUserById(int id) {
        int i = userDao.deleteById(id);
        return i > 0;
    }

    @Override
    public Boolean updateUser(User user,HttpServletRequest request) {
        String newUsername = user.getUsername();
        User isUser = userDao.findUserOnlyByUsername(newUsername);
        if(isUser == null){
            String token = request.getHeader("token");
            int id = JwtUtils.getUserId(token);
            user.setUid(id);
            User oriuser = userDao.getById(id);
            double balance = oriuser.getBalance();
            user.setBalance(balance);
            int i = userDao.updateById(user);
            return i > 0;
        }
        else {
            return false;
        }
    }

    @Override
    public String login(User user) {
        User isuser = userDao.findUserByUsernameAndPassword(user.getUsername(), user.getPassword());
        if(isuser != null)
        {
            int id = isuser.getUid();
            String un = isuser.getUsername();
            String pw = isuser.getPassword();
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("username", un);
            userInfo.put("password", pw);
            String token = JwtUtils.sign(id, userInfo);
            return  token;
        }
        return null;
    }

    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
        User user = userDao.findUserByUsernameAndPassword(username, password);
        return user;
    }

    @Override
    public IPage<User> findUserByName(String name,int currentPage,int pageSize) {
        IPage<User> page = new Page<>(currentPage,pageSize);
        return  userDao.findUserByName(page,name);
    }

    @Override
    public User finUserByUsernameAndPhonenum(String username, String phonenum) {
        return userDao.finUserByUsernameAndPhonenum(username,phonenum);
    }

    @Override
    public Boolean findPassword(String username, String phonenum, String password) {
        User user = userDao.finUserByUsernameAndPhonenum(username, phonenum);
        if(user != null){
            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
            wrapper.eq("username",username).eq("phonenum",phonenum);
            user.setPassword(password);
            return  userDao.update(user, wrapper) > 0;
        }
        else return false;
    }

    @Override
    public int addMoney(double balance, HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("uid",id);
        User user = userDao.getById(id);
        double originbal = user.getBalance();
        user.setBalance(originbal+balance);
        return userDao.update(user,wrapper);
    }

    @Override
    public User findMe(HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        return  userDao.findMe(id);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateById(user);
    }

}
